/* This file is part of SpaceSim.

SpaceSim is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SpaceSim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SpaceSim.  If not, see <http://www.gnu.org/licenses/>. */

#include <SFML/Graphics.hpp>

#include "main.h"

#include "control.h"
#include "graphics.h"
#include "player.h"
#include "time.h"

// the planet the player lands on
int Main::SelectedPlanet = 0;
// the seed / selected system to draw
int Main::SubSystem = 0;
// the actual system the player is in
Cluster Main::CurrentCluster;
// the event the main loop produces
sf::Event Main::Event;
// A (warning) message displayed at the bottom of the screen
sf::String Main::Message("Welcome to SpaceSim!");
// If the game is paused
bool Main::Paused;


// Throw a flagrant error
void Main::Error(std::string ErrorText) {
	Graphics::ErrorString.SetText(ErrorText);
	Graphics::ViewStack.push(Graphics::ViewMode::ErrorView);
}

// Load data
void Main::Init() {
	// Font stuff
	Graphics::ErrorString.SetSize(15);
	Main::Message.SetSize(15);
	
	CurrentCluster.Seed(0);
	LoadShips();
	player.SetShip(&ShipTypes[0]);	// Give the player a ship
	
	//Graphics::App.UseVerticalSync(true);
	
	Graphics::ViewStack.push(Graphics::ViewMode::NoView);		// Last fallback
	Graphics::ViewStack.push(Graphics::ViewMode::ErrorView);	// If we accidentally remove too many views, show an error
	Graphics::ViewStack.push(Graphics::ViewMode::SystemView);	// The main game view
}

int main() {
	// Initialize and load data
	Main::Init();
	
	while (Graphics::App.IsOpened()) {
		// Process events
		while (Graphics::App.GetEvent(Main::Event)) {
			// Close window : exit
			if (Main::Event.Type == sf::Event::Closed)
				Graphics::App.Close();
			if (Main::Event.Type == sf::Event::KeyPressed) {
				KeyPressedHandler(Main::Event.Key.Code);
			}
			if (Main::Event.Type == sf::Event::MouseButtonReleased) {
				MousePressedHandler(Graphics::App.GetInput().GetMouseX(), Graphics::App.GetInput().GetMouseY(), Main::Event.MouseButton.Button);
			}
			if (Main::Event.Type == sf::Event::MouseWheelMoved) {
				Graphics::ZoomFactor *= 1 - Main::Event.MouseWheel.Delta * 0.01;
				if (Graphics::ZoomFactor < 0.01) Graphics::ZoomFactor = 0.01;
			}
		}
		
		// handle more keys
		KeyHandler();
		
		if (Graphics::ViewStack.top() == Graphics::ViewMode::SystemView) {
			Main::Paused = false;
		} else {
			Main::Paused = true;	
		}
		
		// update
		if (!Main::Paused) {
			Time::Update();
			Main::CurrentCluster.GetSystem(Main::SubSystem)->Update();
			player.Update();
		} else {
			Time::FPSClock.Reset();
		}
		
		Graphics::Draw();
	}

    return EXIT_SUCCESS;
}
